package com.yue.sort;

import java.util.Arrays;

/**
 * Created by qiuyang on 2017/9/7.
 */
public class SelectSort {

    public static void main(String[] args) {
//        int[] arr = {5, 8, 3, 1, 4, 5, 5};
//        int[] arr = {5, 4, 3, 2, 1};
        int[] arr = {1, 2, 3, 4, 5};
        print(arr);
        print(sort(arr));
    }

    public static void print(int[] arr) {
        for (int i : arr) {
            System.out.print(i + "  ");
        }
        System.out.println();
    }


    public static int[] sort(int[] a) {
        if (a == null || a.length == 0) {
            return new int[0];
        }

        int[] result = Arrays.copyOf(a, a.length);
        for (int i = 0; i < result.length; i++) {
            int min = result[i];
            int minIndex = i;
            for (int j = i; j < result.length; j++) {
                if (result[j] < min) {
                    min = result[j];
                    minIndex = j;
                }
            }
            result[minIndex] = result[i];
            result[i] = min;
        }
        return result;
    }
}
